<?php
/**
 * FecShop file.
 *
 * @link http://www.fecshop.com/
 * @copyright Copyright (c) 2016 FecShop Software LLC
 * @license http://www.fecshop.com/license/
 */
namespace fecshop\app\apphtml5\modules\Payment\block\paypal\express;
use Yii;
use fec\helpers\CModule;
use fec\helpers\CRequest;
use yii\base\InvalidValueException;
/**
 * @author Terry Zhao <2358269014@qq.com>
 * @since 1.0
 */
class Placeorder {
	
	/**
	 * 用户的账单地址信息，通过用户传递的信息计算而来。
	 */
	public $_billing;
	
	public $_address_id;
	/**
	 * 用户的货运方式
	 */
	public $_shipping_method;
	/**
	 * 用户的支付方式
	 */
	public $_payment_method;
	
	public function getLastData(){
		$post = Yii::$app->request->post();
		if(is_array($post) && !empty($post)){
			foreach($post as $k=>$v){
				$post[$k] = \yii\helpers\Html::encode($v);
			}
			# 设置paypal快捷支付
			$post['payment_method'] = Yii::$service->payment->paypal->express_payment_method;
			# 检查前台传递的数据的完整性
			if($this->checkOrderInfoAndInit($post)){
				# 如果游客用户勾选了注册账号，则注册，登录，并把地址写入到用户的address中
				$save_address_status = $this->updateAddress($post);
				//echo 1;
				if($save_address_status){
					# 更新Cart信息  
					//$this->updateCart();
					# 设置checkout type
					$serviceOrder = Yii::$service->order;
					$checkout_type = $serviceOrder::CHECKOUT_TYPE_EXPRESS;
					$serviceOrder->setCheckoutType($checkout_type);
					# 将购物车数据，生成订单。
					$genarateStatus = Yii::$service->order->generateOrderByCart($this->_billing,$this->_shipping_method,$this->_payment_method);
					//echo 22;
					if($genarateStatus){
						# 得到当前的订单信息
						$doExpressCheckoutReturn = $this->doExpressCheckoutPayment();
						//echo 333;
						if($doExpressCheckoutReturn){
							$ExpressOrderPayment = Yii::$service->payment->paypal->updateExpressOrderPayment($doExpressCheckoutReturn);
							# 如果支付成功，并把信息更新到了订单数据中，则进行下面的操作。
							//echo 444;
							if($ExpressOrderPayment){
								//echo 555;
								# 发送新订单邮件
								
								# 扣除库存和优惠券
								// 在生成订单的时候已经扣除了。参看order service GenerateOrderByCart() function
								
								# 得到支付跳转前的准备页面。
								$paypal_express = Yii::$service->payment->paypal->express_payment_method;
								$successRedirectUrl = Yii::$service->payment->getExpressSuccessRedirectUrl($paypal_express);
								Yii::$service->url->redirect($successRedirectUrl);
								exit;
							}
						}
						//return true;
					}
				}
			}else{
				
			}
		}
		//echo 'eeeeeeee';exit;
		Yii::$service->page->message->addByHelperErrors();
		return false;
	}
	
	public function doExpressCheckoutPayment(){
		$methodName_ 	= "DoExpressCheckoutPayment";
		$nvpStr_ 		= Yii::$service->payment->paypal->getExpressCheckoutPaymentNvpStr();
		//echo $nvpStr_;exit;
		$DoExpressCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_);
		//var_dump($DoExpressCheckoutReturn);
		//exit;
		if(strstr(strtolower($DoExpressCheckoutReturn['ACK']),'success')){
			return $DoExpressCheckoutReturn;
		}else{
			if($DoExpressCheckoutReturn['ACK'] == 'Failure'){
				$message = $DoExpressCheckoutReturn['L_LONGMESSAGE0'];
				# 添加报错信息。
				//Message::error($message);
				Yii::$service->helper->errors->add($message);
			}else{
				Yii::$service->helper->errors->add('paypal express payment error.');
			}
			return false;
		}
	}
	
	/**
	 * @property $post | Array
	 * 登录用户，保存货运地址到customer address ，然后把生成的
	 * address_id 写入到cart中。
	 * shipping method写入到cart中
	 * payment method 写入到cart中 updateCart 
	 */
	public function updateAddress($post){
		return Yii::$service->cart->updateGuestCart($this->_billing,$this->_shipping_method,$this->_payment_method);
	}
	
	/**
	 * 如果是游客，那么保存货运地址到购物车表。
	 */
	/*
	public function updateCart(){
		if(Yii::$app->user->isGuest){
			return Yii::$service->cart->updateGuestCart($this->_billing,$this->_shipping_method,$this->_payment_method);
		}else{
			return Yii::$service->cart->updateLoginCart($this->_address_id,$this->_shipping_method,$this->_payment_method);
		}
	}
	*/
	
	/**
	 * @property $post | Array
	 * @return boolean 
	 * 检查前台传递的信息是否正确。同时初始化一部分类变量
	 */
	public function checkOrderInfoAndInit($post){
		$address_one = '';
		$billing = isset($post['billing']) ? $post['billing'] : '';
		if(!Yii::$service->order->checkRequiredAddressAttr($billing)){
			return false;
		}
		$this->_billing = $billing;
		
		$shipping_method= isset($post['shipping_method']) ? $post['shipping_method'] : '';
		$payment_method = isset($post['payment_method']) ? $post['payment_method'] : '';
		# 验证货运方式
		if(!$shipping_method){
			Yii::$service->helper->errors->add('shipping method can not empty');
			return false;
		}else{
			if(!Yii::$service->shipping->ifIsCorrect($shipping_method)){
				Yii::$service->helper->errors->add('shipping method is not correct');
				return false;
			}
		}
		
		$this->_shipping_method = $shipping_method;
		$this->_payment_method = $payment_method;
		Yii::$service->payment->setPaymentMethod($this->_payment_method);
		return true;
	}
	
	
	
}
